package com.mixsmart.mobile.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import com.mixsmart.commonweb.bean.UserInfo;
import com.mixsmart.commonweb.bean.entity.TCLoginLog;
import com.mixsmart.commonweb.enums.LoginType;
import com.mixsmart.commonweb.helper.HttpRequestHelper;
import com.mixsmart.commonweb.service.IUserService;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.log.ILog;
import com.mixsmart.core.log.LogFactory;
import com.mixsmart.enums.YesNoType;
import com.mixsmart.utils.StringUtils;

/**
 * 登录控制器类
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2016年1月25日
 */
@Controller
@RequestMapping("/login.do")
public class LoginController extends BaseMobileController {
	
	@Autowired
	private IUserService userServ;
	
	@Autowired
	@Qualifier("loginLog")
	private ILog log;
	
	/**
	 * 登录视图
	 * @return 返回登录视图
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelView = new ModelAndView();
		modelView.setViewName("/login");
		return modelView;
	}
	
	/**
	 * 验证用户登录信息；
	 * 如验证成功跳转到首页，否则返回登录界面
	 * @param request Http请求对象
	 * @param username 用户名
	 * @param password 密码
	 * @param imgCode 图像验证码
	 * @return 返回视图
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request, String username, String password, String imgCode) {
		ModelAndView modelView = new ModelAndView();
		ModelMap modelMap = modelView.getModelMap();
		logger.debug("正在验证用户信息...");
		String msg = null;
		if(StringUtils.isNotEmpty(imgCode)) {
			Object sessionCode = HttpRequestHelper.getSession(request, SESSION_CAPTCHA_LOGIN);
			if(null != sessionCode && imgCode.equalsIgnoreCase(sessionCode.toString())) {
				if(StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
					TCLoginLog loginLog = new TCLoginLog();
					loginLog.setUsername(username);
					MixResponse<UserInfo> mixResp = userServ.checkLogin(username, password);
					if(OP_SUCCESS.equals(mixResp.getResult())) {
						loginLog.setState(YesNoType.YES.getIndex());
						setUserInfo2Session(request, mixResp.getData());
					} else {
						msg = "用户名或密码错误";
						loginLog.setState(YesNoType.NO.getIndex());
					}
					//记录登录日志
					
					loginLog.setMsg(mixResp.getMsg());
					mixResp = null;
					loginLog.setIp(HttpRequestHelper.getClientAddr(request));
					loginLog.setUserAgent(HttpRequestHelper.getUserAgent(request));
					loginLog.setLoginType(LoginType.MOBILE.getValue());
					LogFactory.saveLog(log, loginLog);
				} else {
					msg = "请输入用户名或密码";
				}
			} else {
				msg = "验证码输入错误";
			}
		} else {
			msg = "请输入验证码";
		}
		if(null != msg) {
			modelMap.put("msg", msg);
			logger.debug(msg);
			modelView.setViewName("/login");
		} else {
			View view = new RedirectView("/index.do", true, true, false);
			modelView.setView(view);
		}
		return modelView;
	}
	
}
